package com.aaa.cs.service.impl;


import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.cs.dao.CarInsuranceDao;
import com.aaa.cs.entity.CarInsurance;
import com.aaa.cs.service.CarInsuranceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CarInsurance)表服务实现类
 *
 * @author makejava
 * @since 2020-11-18 22:10:48
 */
@Service("carInsuranceService")
public class CarInsuranceServiceImpl implements CarInsuranceService {
    @Resource
    private CarInsuranceDao carInsuranceDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CarInsurance queryById(Integer id) {
        return this.carInsuranceDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<CarInsurance> queryAllByLimit(int offset, int limit) {
        return this.carInsuranceDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param carInsurance 实例对象
     * @return 实例对象
     */
    @Override
    public CarInsurance insert(CarInsurance carInsurance) {
        this.carInsuranceDao.insert(carInsurance);
        return carInsurance;
    }

    /**
     * 修改数据
     *
     * @param carInsurance 实例对象
     * @return 实例对象
     */
    @Override
    public CarInsurance update(CarInsurance carInsurance) {
        this.carInsuranceDao.update(carInsurance);
        return this.queryById(carInsurance.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.carInsuranceDao.deleteById(id) > 0;
    }

//    @Override
//    public Result carid(CarInsurance carInsurance) {
//        List<CarInsurance> carInsurances=carInsuranceDao.queryAll(carInsurance);
//        if (carInsurances!=null&&carInsurances.size()>0){
//            return new Result(ReturnCode.SUCCESSSS.getCode(),ReturnCode.SUCCESSSS.getMsg(),carInsurances);
//        }
//        return new Result(ReturnCode.SUCCESSSS.getCode(),ReturnCode.SUCCESSSS.getMsg(),null);
//    }

    /**
     * 分页查询
     * @param pageNo
     * @param PageSize
     * @param carInsurance
     * @return
     */
    @Override
    public Result queryPageByParam(Integer pageNo, Integer PageSize, CarInsurance carInsurance) {
        if (pageNo==null||PageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //分页
        PageHelper.startPage(pageNo,PageSize);
        PageInfo<CarInsurance>pageInfo=new PageInfo<CarInsurance>(carInsuranceDao.queryAll(carInsurance));
        return new Result(ReturnCode.SUCCESSS.getCode(), ReturnCode.SUCCESSS.getMsg(),pageInfo);
    }

//    @Override
//    public CarInsurance queryPageByParam(String type) {
//        CarInsurance carInsurance=new CarInsurance();
//        //设置参数
//        carInsurance.setType(type);
//        System.out.println(true);
//        //查询
//        List<CarInsurance>carInsurances=carInsuranceDao.queryAll(carInsurance);
//        //判断不空
//        if (carInsurances!=null&&carInsurances.size()>0){
//            return carInsurances.get(0);
//        }
//        return null;
//    }
}